using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;

namespace CNGraphi.U6URP
{
    /// <summary>
    /// 镜头光晕
    /// <para>作者：CN.Graphi</para>
    /// </summary>
    [VolumeComponentMenu("G Post-processing/Lens Flare")]
    public class Lensflare : GPostProcess
    {
        #region 参数
        public Vector2Parameter Center = new Vector2Parameter(new UnityEngine.Vector2(0.5f, 0.5f), false);
        public FocalLengthParameter FocalLength = new FocalLengthParameter(FocalLengthStyle.FL_105, true);
        public FloatParameter FocalScaleFactor = new FloatParameter(0.98f, true);
        public FloatParameter StartOffset = new FloatParameter(-0.42f,  true);
        public ClampedFloatParameter Range = new ClampedFloatParameter(0.65f, 0.0f, 1.0f, false);
        public MinFloatParameter FadeoutPow = new MinFloatParameter(0.25f, 0.0f, false);
        public ClampedFloatParameter Intensity = new ClampedFloatParameter(0.0f, 0.0f, 2.0f, true);
        #endregion


        public override bool IsActive() => mMaterial != null && Intensity.value != 0.0f;


        internal override void OnRender(CommandBuffer cmd, PostProcessPassData passData, TextureHandle src, TextureHandle dst)
        {
            if (mMaterial == null) { return; }

            mMaterial.SetFloat("_Intensity", Intensity.value);
            mMaterial.SetFloat("_FocalLength", (float)FocalLength.value);
            mMaterial.SetFloat("_FocalScaleFactor", FocalScaleFactor.value);
            mMaterial.SetFloat("_StartOffset", StartOffset.value);
            mMaterial.SetFloat("_Range", Range.value);
            mMaterial.SetFloat("_FadeoutPow", FadeoutPow.value);
            mMaterial.SetVector("_Center", Center.value);

            Draw(cmd, src, dst, 0);
        }


        internal override void OnTextureCreate(RenderGraph renderGraph, ContextContainer frameData)
        {
        }

        internal override void OnTextureUseCommit(IUnsafeRenderGraphBuilder builder)
        {
        }

        protected override void OnDispose()
        {
        }
    }

}